/*
由原先与CCD290_V6配套的DACx0508_MAIN.c修改而来,去掉菊花链功能,仅保留给单独DACx0508芯片配置的功能.

#  对DAC70508芯片进行配置，从配置文件中读取参数之后进行配置: 
#  dac70508_cfg mode dac70508_cfg.ini state
*/

#include "SchmidtCAM.h"

int main( int argc, char *argv[] ){

	if( argc != 3 ) {
	    printf("usage:\n");
	    printf("\t%s dacx0508_default.ini state\n", argv[0]);
		printf("\t state = 0 (off) or state = 1 (on)\n");
	    return 0;
	}
	
	int state= atoi(argv[2]);
	
	if( (state !=0) && (state != 1) ){
		char errmsg[1024];
	    sprintf(errmsg," state MUST be 0 or 1 !");
	    printDebugInfo(errmsg);
	    exit(0);
	}

	int fd = open("/dev/mem", O_RDWR | O_SYNC);
	uint *ptr1 = mmap(NULL, 10, PROT_READ | PROT_WRITE, MAP_SHARED, fd, ADDR_GPIO_OUT);
	uint *gpio_reg 		= ptr1;
	uint *gpio2_reg		= ptr1+2;	
	
	uint *ptr2 = mmap(NULL, 10, PROT_READ | PROT_WRITE, MAP_SHARED, fd, ADDR_GPIO_IN);
	uint *gpio_in_reg	= ptr2;

	// SPI config
    SPI_CONFIG *spi_config = (SPI_CONFIG*)malloc(sizeof(SPI_CONFIG));
    
    setup_SPI_by_device_name( spi_config, "dacx0508" );
    
	DACx0508_Config *dac70508_cfg = (DACx0508_Config*)malloc(sizeof(DACx0508_Config));
	init_dacx0508_config( dac70508_cfg );

    char inifile[1024];
    sprintf(inifile, "%s", argv[1]);
    read_dacx0508_config( dac70508_cfg, inifile, "DACX0508" );

#ifdef _DEBUG_DACX0508_
	print_dacx0508_config( dac70508_cfg );
#endif

	// exit(0);

	config_dacx0508_via_SPI( gpio_reg, 
					         gpio2_reg, 
					         gpio_in_reg,
					         spi_config,
					         dac70508_cfg,
							 state );
    
    free(spi_config);
    
    munmap(ptr1,10);
    munmap(ptr2,10);
    close(fd);
}
